package com.hucii.app.activity;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.Toast;

import com.hucii.app.R;
import com.hucii.app.adapter.GallayAdapter;
import com.hucii.app.adapter.GalleryFlow;
import com.hucii.app.adapter.ImageAdapter;
import com.hucii.app.utils.BitmapUtil;

public class OrderByPictureActivity extends BaseActivity implements
		OnItemSelectedListener {

	private GalleryFlow galleryFlow;
	private ImageAdapter imageAdapter;
	private GallayAdapter gallayAdapter;
	private Gallery gallery;
	
	ArrayList<BitmapDrawable> mBitmaps = new ArrayList<BitmapDrawable>();

	private Integer[] images = { R.drawable.pic11, R.drawable.pic12,
			R.drawable.pic13, R.drawable.pic14, R.drawable.pic15,
			R.drawable.pic11, R.drawable.pic12 };

	private Integer[] mImageIds = { R.drawable.pic11, R.drawable.pic12,
			R.drawable.pic13, R.drawable.pic14, R.drawable.pic15,
			R.drawable.pic11, R.drawable.pic12 };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setView(R.layout.order_by_picture);

		initData();
		initView();
		registerListener();
		display();
	}

	private void initData() {
		generateBitmaps();
	}

	private void initView() {
		gallery = (Gallery) this.findViewById(R.id.gallery);
		galleryFlow = (GalleryFlow) this.findViewById(R.id.gallery_flow);

		gallayAdapter = new GallayAdapter(this, mImageIds);
		gallayAdapter.createImages();
		gallery.setSpacing(25);
		
		
		galleryFlow.setBackgroundColor(Color.BLACK);	//设置背景色为黑色
		galleryFlow.setSpacing(50);	//图片间隔为50
		galleryFlow.setFadingEdgeLength(0);
		galleryFlow.setGravity(Gravity.CENTER_VERTICAL);
		galleryFlow.setAdapter(new GalleryAdapter1());

		imageAdapter = new ImageAdapter(this, images);
		imageAdapter.createReflectedImages();// 创建倒影效果
		galleryFlow.setFadingEdgeLength(0);
		galleryFlow.setSpacing(25); // 图片之间的间距
	}

	private void registerListener() {
		galleryFlow.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(getApplicationContext(),
						String.valueOf(position), Toast.LENGTH_SHORT).show();
			}
		});
	}

	private void display() {
		galleryFlow.setAdapter(imageAdapter);
		galleryFlow.setSelection(4);
		galleryFlow.setOnItemSelectedListener(this);

		gallery.setAdapter(gallayAdapter);
		gallery.setSelection(4);
		gallery.setOnItemSelectedListener(this);

	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long arg3) {
		if (parent.getAdapter().equals(gallayAdapter)) {
			galleryFlow.setSelection(position);
			imageAdapter.notifyDataSetChanged();
		} else if (parent.getAdapter().equals(imageAdapter)) {
			gallery.setSelection(position);
			gallayAdapter.notifyDataSetChanged();
		}

	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {

	}
	
	private void generateBitmaps() {
		int[] ids = { R.drawable.pic11, R.drawable.pic12,
				R.drawable.pic13, R.drawable.pic14, R.drawable.pic15,
				R.drawable.pic11, R.drawable.pic12 };

		for (int id : ids) {
			Bitmap bitmap = createReflectedBitmapById(id);
			if (null != bitmap) {
				BitmapDrawable drawable = new BitmapDrawable(bitmap);
				drawable.setAntiAlias(true);
				mBitmaps.add(drawable);
			}
		}
	}

	private Bitmap createReflectedBitmapById(int resId) {
		Drawable drawable = getResources().getDrawable(resId);
		if (drawable instanceof BitmapDrawable) {
			Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
			Bitmap reflectedBitmap = BitmapUtil.createReflectedBitmap(bitmap);

			return reflectedBitmap;
		}

		return null;
	}
	
	private class GalleryAdapter1 extends BaseAdapter {
		@Override
		public int getCount() {
			return mBitmaps.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (null == convertView) {
				convertView = new MyImageView(OrderByPictureActivity.this);
				convertView.setLayoutParams(new Gallery.LayoutParams(110, 184));
			}

			ImageView imageView = (ImageView) convertView;
			imageView.setImageDrawable(mBitmaps.get(position));
			imageView.setScaleType(ScaleType.FIT_XY);

			return imageView;
		}
	}

	private class MyImageView extends ImageView {
		public MyImageView(Context context) {
			this(context, null);
		}

		public MyImageView(Context context, AttributeSet attrs) {
			super(context, attrs, 0);
		}

		public MyImageView(Context context, AttributeSet attrs, int defStyle) {
			super(context, attrs, defStyle);
		}

		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
		}
	}

}
